package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.ManagerRole;
import com.woniu.entity.Role;
import com.woniu.entity.RoleUrlPermission;
import com.woniu.entity.UrlPermission;
import com.woniu.mapper.ManagerRoleMapper;
import com.woniu.mapper.RoleMapper;
import com.woniu.mapper.RoleUrlPermissionMapper;
import com.woniu.mapper.UrlPermissionMapper;
import com.woniu.service.RoleService;
import com.woniu.service.vo.RoleUrlVo;
import com.woniu.service.vo.TestPageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author D
 * @since 2023-03-27 08:24:57
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    RoleUrlPermissionMapper roleUrlPermissionMapper;
    @Resource
    UrlPermissionMapper urlPermissionMapper;
    @Resource
    ManagerRoleMapper managerRoleMapper;

    /**
     * 分页查询角色，通过角色id查询关联表，判断角色是否有权限，如果有再查询相关权限，存入roleUrlVo
     *
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public TestPageVo selectList(Integer currentPage, Integer pageSize, String keyword) {
        TestPageVo pageVo = new TestPageVo<>();
        List<RoleUrlVo> roleUrlVoList = new ArrayList<>();
        // 分页查询
        Page<Role> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Role> roleWrapper = new QueryWrapper<>();
        if (keyword != null || !keyword.equals("")) {
            roleWrapper.like("name", keyword);
        }
        Page<Role> rolePage = roleMapper.selectPage(page, roleWrapper);
        List<Role> roleList = rolePage.getRecords();
        // 遍历角色列表，查询角色是否有权限
        for (Role role : roleList) {
            RoleUrlVo roleUrlVo = new RoleUrlVo();
            QueryWrapper<RoleUrlPermission> roleUrlPermissionWrapper = new QueryWrapper<>();
            roleUrlPermissionWrapper.eq("role_id", role.getId());
            List<RoleUrlPermission> roleUrlPermissionList = roleUrlPermissionMapper.selectList(roleUrlPermissionWrapper);
            // 如果有权限，查询权限列表
            if (roleUrlPermissionList.size() > 0) {
                ArrayList<UrlPermission> urlPermissionList = new ArrayList<>();
                for (RoleUrlPermission roleUrlPermission : roleUrlPermissionList) {
                    urlPermissionList.add(urlPermissionMapper.selectById(roleUrlPermission.getUrlPermissionId()));
                }
                roleUrlVo.setPermissions(urlPermissionList);
            }
            roleUrlVo.setId(role.getId()).setCode(role.getCode())
                    .setName(role.getName()).setInsertTime(role.getInsertTime())
                    .setUpdateTime(role.getUpdateTime());
            roleUrlVoList.add(roleUrlVo);
        }
        // 封装pageVo
        pageVo.setCurrentPage(rolePage.getCurrent()).setSize(rolePage.getSize())
                .setTotal(rolePage.getTotal()).setPageVo(roleUrlVoList);
        return pageVo;
    }

    /**
     * 删除角色同时删除其与权限绑定关系
     *
     * @param id
     */
    @Override
    public void deleteRole(Long id) {
        roleMapper.deleteById(id);
        // 删除角色与权限绑定关系
        QueryWrapper<RoleUrlPermission> roleUrlWrapper = new QueryWrapper<>();
        roleUrlWrapper.eq("role_id", id);
        roleUrlPermissionMapper.delete(roleUrlWrapper);

        QueryWrapper<ManagerRole> managerRoleWrapper = new QueryWrapper<>();
        managerRoleWrapper.eq("role_id", id);
        managerRoleMapper.delete(managerRoleWrapper);
    }
}
